<?php
/**
 * Login.php
 * 
 * @category    Madoqua
 * @package     Validate
 * @subpackage  Login
 */

/** base class */
require_once 'Zend/Validate/Abstract.php';

/**
 * Madoqua_Validate_Login
 * 
 * @category    Madoqua
 * @package     Validate
 * @subpackage  Login
 */
class Madoqua_Validate_Login extends Zend_Validate_Abstract 
{
    /**
     * auth instance
     *
     * @var Zend_Auth
     */
    private $_auth;    
    
    /**
     * auth adapter
     *
     * @var Zend_Auth_Adapter_DbTable
     */
    private $_adapter;
    
    /**
     * constructor
     * 
     * @param array $options
     */
    public function __construct($options = null) {
        if (!isset($options['auth'], $options['adapter'])) {
            throw new Exception('auth or auth adapter not passed to $options');
        }
        //check for right options
        
        $this->_auth = $options['auth'];
        $this->_adapter = $options['adapter'];
        //auth and adapter
    }

    /**
     * validate login
     *
     * @param string $value
     */
    public function isValid($value) {
        
        if (strlen($value) == 0) {
            return false;
        }
        //do not accept empty login attempts
        
        $args = func_get_args();
        if (!isset($args[1]))
        {
            return false;
        }
        $values = $args[1];
        //values from the form (this validator won't work on it's own)

        $this->_adapter->setCredential(md5($values['password']));
        $this->_adapter->setIdentity($values['username']);
        $this->_auth->authenticate($this->_adapter);
        //try to authenticate
        
        return $this->_auth->hasIdentity();
        //has it succeeded?        
    }
}